package com.wertop.viid.controller;

import com.wertop.viid.common.annotation.LogOperation;
import com.wertop.viid.common.annotation.RequireAuth;
import com.wertop.viid.common.enums.InfoKind;
import com.wertop.viid.common.result.ConfirmStatusType;
import com.wertop.viid.common.result.ResponseStatusListObject;
import com.wertop.viid.common.result.ResponseStatusObject;
import com.wertop.viid.common.validator.ValidatorUtils;
import com.wertop.viid.dto.PersonListObject;
import com.wertop.viid.dto.PersonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description PersonController
 * @Author yempty
 * @Date 2020/6/18 11:27
 */
@RestController
@RequestMapping("Persons")
@Api(tags = "人员相关接口")
public class PersonController {

    @RequireAuth
    @PostMapping
    @ApiOperation("人员批量新增")
    @LogOperation("人员批量新增")
    public ResponseStatusListObject register(@RequestBody PersonListObject personListObject) {

        List<ResponseStatusObject> list = new ArrayList<>(personListObject.getPersonObjects().size());
        ResponseStatusObject responseStatusObject;
        String errMsg;
        InfoKind infoKind;
        for (PersonObject personObject : personListObject.getPersonObjects()) {
            // 检验参数
            errMsg = ValidatorUtils.validateEntity(personObject);
            if (StringUtils.isBlank(errMsg)) {
                infoKind = InfoKind.getByValue(personObject.getInfoKind());
                if (infoKind == null) {
                    errMsg += "信息分类的值不符合要求.";
                } else {
                    switch (infoKind) {
                        case AUTO:
                            if (StringUtils.isBlank(personObject.getDeviceId())) {
                                errMsg += "自动采集，设备编码必须填写.";
                            }
                            if (personObject.getLeftTopX() == null || personObject.getLeftTopY() == null
                                    || personObject.getRightBtmX() == null || personObject.getRightBtmY() == null) {
                                errMsg += "自动采集，记录人轮廓矩形框的左上角坐标和右下角坐标必须填写.";
                            }
                            break;
                        case MANUAL:
                            if (personObject.getLocationMarkTime() == null) {
                                errMsg += "人工采集，位置标记时间必须填写.";
                            }
                            if (personObject.getPersonAppearTime() == null) {
                                errMsg += "人工采集，出现时间必须填写.";
                            }
                            if (personObject.getPersonDisAppearTime() == null) {
                                errMsg += "人工采集，消失时间必须填写.";
                            }
                            if (personObject.getIsDriver() == null) {
                                errMsg += "人工采集，是否驾驶员必须填写.";
                            }
                            if (personObject.getIsDetainees() == null) {
                                errMsg += "人工采集，是否在押人员必须填写.";
                            }
                            if (personObject.getIsVictim() == null) {
                                errMsg += "人工采集，是否被害人必须填写.";
                            }
                            if (personObject.getIsSuspiciousPerson() == null) {
                                errMsg += "人工采集，是否可疑人必须填写.";
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            if (StringUtils.isBlank(errMsg)) {
                responseStatusObject = ResponseStatusObject.create(personObject.getPersonId(), ConfirmStatusType.OK);
            } else {
                responseStatusObject = ResponseStatusObject.create(personObject.getPersonId(), ConfirmStatusType.Validate_Error.getCode(), errMsg);
            }
            list.add(responseStatusObject);
        }

        return ResponseStatusListObject.create(list);
    }
}
